/**
 * StarNode.java
 *
 * Roslan's Remake of MOO in 3D using Ardor3D
 * (C) 2010-2012 Roslan Amir. All rights reserved.
 */

package name.roslan.games.moo3d.spatial;

import java.awt.Point;

import name.roslan.games.moo3d.data.Star;
import name.roslan.games.moo3d.utils.BMFontLoader;
import name.roslan.games.moo3d.utils.HexGridUtils;

import com.ardor3d.math.type.ReadOnlyColorRGBA;
import com.ardor3d.scenegraph.Node;
import com.ardor3d.ui.text.BMText;
import com.ardor3d.ui.text.BMText.AutoFade;
import com.ardor3d.ui.text.BMText.AutoScale;

/**
 * Comments...
 * 
 * @author Roslan Amir
 * @version 1.0 - May 3, 2012
 */
public class StarNode extends Node {

    /**
     * 
     */
    private static final double STARNAME_OFFSET_Y = -1.25;

    /**
     * The indexes for the tile where this star is in the 2-dimensional array representing the galaxy map.
     */
    public final Point tile = new Point();

    /**
     * The range (in parsecs) of this star/colony from the nearest player's colony. (0 if this is a player colony)
     */
    public int range;

    // /**
    // * The list of <code>FleetNode</code>s orbiting this star.
    // */
    // private ArrayList<FleetNode> orbiting;

    // /**
    // * The list of <code>FleetNode</code>s departing from this star.
    // */
    // private ArrayList<FleetNode> departing;

    /**
     * Creates an instance of the <code>StarNode</code> class.
     * 
     * @param star - the <code>Star</code> instance for this <code>StarNode</code>
     * @param x - the x coordinate of the star's tile on the galaxy map
     * @param y - the y coordinate of the star's tile on the galaxy map
     */
    public StarNode(Star star, int x, int y) {
        super("StarNode<" + star.name + '>');
        this.tile.x = x;
        this.tile.y = y;
        this.range = Integer.MAX_VALUE;
        // this.orbiting = null;
        // this.departing = null;
        this.setUserData(star);
    }

    /**
     * Sets the range (in parsecs) of this star from the given player's colony. The range is updated only if it is less than the current value.
     * 
     * @param playerColony - the player's colony from where to calculate the range
     */
    public void setRange(StarNode playerColony) {
        int dist = HexGridUtils.distance(playerColony.tile, this.tile);
        if (dist < this.range) {
            this.range = dist;
        }
    }

    /**
     * Add the name of the star to this node. This is done is for explored star.
     * 
     * @param textColor - the color of the text (based on the race color)
     */
    public void addStarNameText(ReadOnlyColorRGBA textColor) {
        String name = ((Star) this.getUserData()).name;
        final BMText text = new BMText("BMText<" + name + '>', name, BMFontLoader.getDefaultFont(), BMText.Align.Center, BMText.Justify.Center);
        text.setFontScale(0.95);
        // text.setSolidColor(null);
        text.setDefaultColor(textColor);
        // text.setAutoFadeDistanceRange(nearOpaque, farTransparent);
        text.setAutoFade(AutoFade.Off);
        text.setAutoScale(AutoScale.FixedScreenSize);
        text.setAutoRotate(true);
        text.setTranslation(0, STARNAME_OFFSET_Y, 0);
        this.attachChild(text);
    }

    /**
     * 
     */
    public void addStarBillboard() {
    }

    /**
     * Return the <code>Star</code> instance embedded within this <code>StarNode</code>.
     * 
     * @return the <code>Star</code> instance embedded within this <code>StarNode</code>
     */
    public Star getStar() {
        return (Star) this.getUserData();
    }

}
